package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"shopping/appconfig"
)

var (
	Db  *gorm.DB
	err error
)

func Init() {

	config := appconfig.AppConfig.Mysql

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True", config.User, config.Password, config.Host, config.Port, config.Database)
	Db, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("mysql连接失败")
	} else {
		fmt.Println("mysql连接成功")
	}
	Db.AutoMigrate(&User{}, &Product{})
}
